package com.dehui.traders.ui.msg;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.dehui.traders.R;
import com.dehui.traders.bean.BaseListResponse;
import com.dehui.traders.bean.BaseResponse;
import com.dehui.traders.bean.MsgBean;
import com.dehui.traders.bean.PlateListBean;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.ui.market.rank.RankAdapter;
import com.dehui.traders.ui.setting.SettingActivity;
import com.dehui.traders.utils.UserHelp;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class MsgListActivity extends AppCompatActivity {

    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private List<MsgBean> list = new ArrayList<>();
    private MsgListAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_list);
        initToolbar("我的", "我的消息");
        initViews();
        initData();
    }

    public static void start(Context context){
        Intent intent = new Intent(context,MsgListActivity.class);
        context.startActivity(intent);
    }

    private void initData() {

        Call<BaseResponse<List<MsgBean>>> call = NetImpl.pushMessage(UserHelp.getUserId());
        call.enqueue(new Callback<BaseResponse<List<MsgBean>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<MsgBean>>> call, Response<BaseResponse<List<MsgBean>>> response) {
                BaseResponse<List<MsgBean>> body = response.body();
                if (body.status == 200){
                    List<MsgBean> result = body.result;
                    list = result;
                    adapter.refresh(list);
                    adapter.notifyDataSetChanged();
                }
                refreshLayout.finishRefresh();
            }

            @Override
            public void onFailure(Call<BaseResponse<List<MsgBean>>> call, Throwable t) {
                refreshLayout.finishRefresh();
            }
        });
    }

    private void initViews() {
        refreshLayout = findViewById(R.id.refresh_view);
        recyclerView = findViewById(R.id.recycler_view);

        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                initData();
            }
        });
        adapter = new MsgListAdapter(list,this);
        recyclerView.setAdapter(adapter);
    }

    public void initToolbar(String leftName, String title) {
        View toolbar = findViewById(R.id.toolbar);
        toolbar.findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        TextView tvTitle = toolbar.findViewById(R.id.tv_title);
        tvTitle.setText(title);

        TextView tvBackName = toolbar.findViewById(R.id.tv_back_name);
        tvBackName.setText(leftName);
        tvBackName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

}
